<?php

namespace app\api\controller;

use app\Request;
use think\facade\Db;
use think\facade\Env;
use think\response\Json;
use app\services\user\UserServices;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use app\api\validate\public\SendCodeValidate;
use app\services\public\SendCodeServices;
use think\db\exception\{DataNotFoundException, DbException, ModelNotFoundException};

/**
 * app师傅端
 * Class PublicController
 * @package app\api\controller
 */
class PublicController extends RestBaseController
{
    /** @var SendCodeServices */
    protected $services;

    /**
     * @return void
     */
    protected function initialize()
    {
        parent::initialize();
        $this->services = app()->make(SendCodeServices::class);
    }

    /**
     * 发送短信验证码
     * @param Request $request
     * @return Json
     */
    public function send(Request $request): Json
    {
        $data = $request->postMore([
            ['mobile', ''],
        ]);

        try {
            validate(SendCodeValidate::class)->scene('send_code')->check($data);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }

        try {
            $res = $this->services->send($data['mobile']);
        } catch (AdminException $e) {
            $this->error($e->getMessage());
        }

        $this->success("发送短信验证码成功!", $res);
    }
}
